<?php
/*
 * Pulse module version 0.1
 */

/**
 *
 * @author ixidor
 */
class pulseController extends kseController{
    public $name = "pulseController";
    public $metaTemplate = NULL;
    
    public function __construct(){
                
        self::info("Initialized");
        if(!$this->kseInclude(Core::get()->propertyList["PATH_MODULES"] ."pulse/dicts/". Core::get()->propertyList["SYSTEM_LANG"] .".php", "Including ". Core::get()->propertyList["SYSTEM_LANG"] ." dictionary")){
            $this->kseRequire(Core::get()->propertyList["PATH_MODULES"] ."pulse/dicts/ru.php", "Including default dictionary");
        }
        
        $this->parseFile(Core::get()->propertyList["PATH_MODULES"] . "pulse/config.xml");
        
        $this->getNamespaces();
        
        $this->transferNamespaces();
        
    }
    
    public function getNamespaces(){
        //admin login page url
        $this->namespace[] = $this->propertyList["PATH_CATALOGUE"];
    }
    
    public function showSysPage(){
        //@todo system pages for pulse module
        echo "Hello Sys page";
    }
    
    public function makeResult($objects){
        //including model
        $this->kseRequire($_SERVER['DOCUMENT_ROOT'] ."/". Core::get()->propertyList["PATH_MODULES"] ."pulse/model.php", "Accesing pulse model");
        //initialize model
        $this->model = new pulseModel();
        
        foreach ($objects as $object) {
            $this->metaTemplate = $this->getContentFromURL(Core::get()->propertyList["PATH_MODULES"] ."pulse/meta/". $object->getName() .".php");
            $this->getFromDB($object->getName());
            //get result from model
            $object->setContent($this->model->getResult($object));
        }

    }
    
    public function getFromDB($case){
        switch ($case){
            case "root_categories":
                $query = "SELECT node.cat_id, node.cat_name, node.cat_link, (COUNT(parent.cat_name) - (sub_tree.depth + 1)) AS depth
		FROM kpulse_categories AS node,
			kpulse_categories AS parent,
			kpulse_categories AS sub_parent,
			(
				SELECT node.cat_name, (COUNT(parent.cat_name) - 1) AS depth
				FROM kpulse_categories AS node,
				kpulse_categories AS parent
				WHERE node.cat_lft BETWEEN parent.cat_lft AND parent.cat_rgt
				AND node.cat_name = 'ROOT'
				GROUP BY node.cat_name
				ORDER BY node.cat_lft
			)AS sub_tree
		WHERE node.cat_lft BETWEEN parent.cat_lft AND parent.cat_rgt
			AND node.cat_lft BETWEEN sub_parent.cat_lft AND sub_parent.cat_rgt
			AND sub_parent.cat_name = sub_tree.cat_name
		GROUP BY node.cat_name
		HAVING depth = 1
		ORDER BY node.cat_lft";
                
                $this->result = mysql_query($query) or Core::get()->kseDie(mysql_error());
            break;
        }
    }

}
?>